import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_oldman_health/models/login_model.dart';
import 'package:flutter_oldman_health/models/response_data_model.dart';
import 'package:flutter_oldman_health/navigator/tab_navigator.dart';
import 'package:flutter_oldman_health/utils/encrypt_util.dart';
import 'package:flutter_oldman_health/utils/http_util.dart';
import 'package:flutter_oldman_health/utils/utils.dart';

import 'constant.dart';

class AuthService {
  static Future login2(BuildContext context, String phone, String password,
      String toastStr, bool register) async {
    var random = Random();
    int randomInt = random.nextInt(100);
    randomInt = (randomInt * 10).ceil();
    String pass1 = EncryptUtil.encodeMd5(password + "!%&qishiyun110");
    pass1 = EncryptUtil.encodeMd5(pass1 + randomInt.toString());
    print(("登录时生产的密码： $pass1"));

    var response = await AuthService.login(phone, randomInt, pass1);
    if (response != null && response.statusCode == 200) {
      LoginModel value1 = LoginModel.fromJson(response.data);
      if (value1.success()) {
        print(value1.data?.tokenInfo?.token);
        print(value1.data?.user?.id);

        EasyLoading.showSuccess(toastStr);
        Utils.putStrSharePreferences("phone", phone);
        Utils.putStrSharePreferences("password", password);
        Utils.putStrSharePreferences("token", value1.data?.tokenInfo?.token);
        Utils.putStrSharePreferences("userId", value1.data?.user?.id);
        Utils.putStrSharePreferences("user_nick", value1.data?.user?.nick);

        Navigator.pop(context);
        if (register) {
          Navigator.of(context).pushReplacement(
              MaterialPageRoute(builder: (context) => TabNavigation()));
        } else {
          Navigator.push(context,
              MaterialPageRoute(builder: (context) => TabNavigation()));
        }
        return true;
      } else {
        EasyLoading.showInfo(value1.msg);
      }
    } else {
      EasyLoading.showInfo("服务器连接失败，请检测网络");
    }
    return false;
  }

  static Future login(String username, int randomInt, String password) async {
    var result = await HttpUtil.post(
      Constant.host + '/manager/v3/user/login',
      data: {
        "name": username,
        "random": randomInt,
        "pass": password,
        "qut": "mobile",
        "type": 2
      },
    );

    return result;
  }

  static Future<dynamic> checkoutUserInfo(String phone) async {
    var result = await HttpUtil.get(Constant.host +
        '/manager/medical/user/${Constant.enterpriseId}/status/$phone');
    if (result != null && result.statusCode == 200) {
      ResponseDataModel<int> value1 = ResponseDataModel.fromJson(result.data);
      return value1;
    }
    return null;
  }

  static Future register(
      String name, String phone, String pass, String code) async {
    String encryptPsw = EncryptUtil.encodeMd5(pass + '!%&qishiyun110');

    var result = await HttpUtil.post(
      Constant.host + '/manager/medical/user/${Constant.enterpriseId}/register',
      data: {"name": name, "phone": phone, "pass": encryptPsw, "code": code},
    );
    return result;
  }

  static Future requestVerificationCode(String phone) async {
    var result = await HttpUtil.get(
        Constant.host + '/manager/medical/user/verification_code/$phone');

    return result;
  }

  static Future resetPsw(String phone, String pass, String code) async {
    var result = await HttpUtil.put(
      Constant.host + '/manager/medical/user/${Constant.enterpriseId}/reset',
      data: {"phone": phone, "pass": pass, "code": code},
    );
    return result;
  }

  static Future changePsw(
      String phone, String pass, String newpass, String code) async {
    var result = await HttpUtil.put(
      Constant.host +
          '/manager/medical/user/${Constant.enterpriseId}/modification/pass',
      data: {"phone": phone, "pass": pass, "newPass": newpass, "code": code},
    );
    return result;
  }

  static Future queryUserInfo() async {
    var result = await HttpUtil.get(
        Constant.host + '/manager/medical/user/${Constant.enterpriseId}');
    return result;
  }

  static Future changeBindPhone(
      String oldPhone, String oldCode, String newPhone, String newCode) async {
    var result = await HttpUtil.put(
        Constant.host +
            '/manager/medical/user/${Constant.enterpriseId}/verification',
        data: {
          "oldPhone": oldPhone,
          "oldCode": oldCode,
          "newPhone": newPhone,
          "newCode": newCode
        });
    return result;
  }

  //设备注册
  //deviceCode	是	String	 	设备串号；如果没有，传mac
  // mac	否	String	 	设备mac地址
  static Future registerDevice(String deviceCode, String mac) async {
    var result = await HttpUtil.post(
      Constant.host + '/manager/medical/user/device/${Constant.enterpriseId}',
      data: {"deviceCode": deviceCode, "mac": mac},
    );
    return result;
  }

  static Future logout() async {
    var result =
        await HttpUtil.delete(Constant.host + '/manager/user/login-info');
    return result;
  }

  static Future cancellation() async {
    var result = await HttpUtil.delete(Constant.host +
        '/manager/medical/user/${Constant.enterpriseId}/cancel');
    return result;
  }

//
// static Future checkSession() async {
//   var result = await HttpUtil.get(Constant.host + 'check');
//
//   return result;
// }
}
